<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
	  <el-form-item label="楼宇" prop="floorName">
	    <el-select v-model="queryParams.floorName"
	     class="width100"
	     placeholder="楼宇" clearable>
	      <el-option
	       v-for="item in ListFloorArr"
	       :key="item.id"
	       :label="item.floorName"
	       :value="item.id"
	      />
	    </el-select>
	  </el-form-item>
	  <el-form-item label="房屋号" prop="roomNo">
	    <el-input
	      v-model="queryParams.roomNo"
	      placeholder="请输入房屋号"
	      clearable
	    />
	  </el-form-item>
	  <el-form-item label="挂租状态" prop="status">
	    <el-select v-model="queryParams.status"
	     class="width100"
	     placeholder="挂租状态" clearable>
	      <el-option
	       v-for="item in rentStatus"
	       :key="item.type"
	       :label="item.name"
	       :value="item.type"
	      />
	    </el-select>
	  </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:post:add']"
        >新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="postList" 
		
	> 
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column label="序号" align="center" prop="id" />
      <el-table-column label="房屋名称" align="center" prop="room" />
      <el-table-column label="楼宇名称" align="center" prop="floorName" />
      <el-table-column label="单元数" align="center" prop="units" />
      <el-table-column label="楼层数" align="center" prop="floor" />
      <el-table-column label="房屋数" align="center" prop="roomNo" />
      <el-table-column label="建筑面积" align="center" prop="buildArea" />
      <el-table-column label="状态" align="center">
		  <template slot-scope="scope">
			  {{ scope.row.rentStatusCn }}
<!-- 			  <el-tooltip class="item" effect="dark" :content="scope.row.reviewComment" placement="bottom-start">
				<i class="el-icon-info" v-if='scope.row.roomStatus == 9'></i>
			  </el-tooltip> -->
		  </template>
	  </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleUpdate(scope.row)"
          >查看</el-button>
		  <el-button
		    size="mini"
		    type="text"
		    icon="el-icon-edit"
		    @click="handleUpdate(scope.row)"
		    v-hasPermi="['system:post:edit']"
		  >修改</el-button>
		  <el-button
		    size="mini"
			v-if='scope.row.roomStatus == 3 || scope.row.roomStatus == 9'
		    type="text"
		    icon="el-icon-delete"
		    @click="handleEditStatus(scope.row)"
		    v-hasPermi="['system:post:remove']"
		  >审核</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:post:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改岗位对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="房屋" prop="roomId">
          <el-select v-model="form.roomId"
           class="width100"
           placeholder="房屋" clearable>
            <el-option
              v-for="item in RoomList"
              :key="item.id"
              :label="item.communityName +''+ item.floorName +''+ item.units +'单元'+ item.roomNo +'号'"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
		<el-form-item label="售价" prop="roomPrice">
		  <el-input placeholder="请输入售价" type='number' v-model="form.roomPrice">
		     <template slot="append">元</template>
		  </el-input>
		</el-form-item>
		<el-form-item label="可住人数" prop="occupancy">
		  <el-input placeholder="请输可住人数" v-model="form.occupancy" type='number'>
		  </el-input>
		</el-form-item>
		<el-form-item label="建筑面积" prop="buildArea">
			<el-input placeholder="请输入建筑面积" type='number' v-model="form.buildArea">
			    <template slot="append">㎡</template>
			  </el-input>
		</el-form-item>
		<el-form-item label="介绍图片" prop="images">
			<FileUpload
				@input='inputFile'
				:value="fileList"
				:fileType="['png', 'jpg', 'jpeg']"
			>
			</FileUpload>
		</el-form-item>
		<el-form-item label="图文说明" prop="noteDesc">
			<editor v-model="form.noteDesc" :min-height="192"/>
		</el-form-item>
        <el-form-item label="简介" prop="remark">
			<el-input v-model="form.remark" placeholder="备注" type="textarea" />
        </el-form-item>
		<el-form-item label="设施配套" required>
			<div class="shepei">
				<el-checkbox-group
				      v-for="(item, index) in supportServices"
				      v-model="item.checked"
				      :key="index"
				      @change="checkboxChange"
				    >  
					<span class="font14 typeitem">{{item.type}}</span>
				      <el-checkbox
				        v-for="(jitem, jindex) in item.item"
				        :label="jitem"
				        :key="jindex"
				        >{{ jitem }}</el-checkbox
				      >
				    </el-checkbox-group>
			</div>
		</el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
	<!--挂租审核-->
	<!-- 添加或修改岗位对话框 -->
	<el-dialog title="挂租审核" :visible.sync="openStatus" width="500px" append-to-body>
	  <el-form ref="formStaus" :model="formStaus" :rules="rulesStatus" label-width="80px">
	    <el-form-item label="托管周期" prop="custodyCycle">
	      <el-date-picker
	        v-model="formStaus.custodyCycle"
	        value-format="yyyy-MM-dd"
	        type="daterange"
	        range-separator="-"
	        start-placeholder="开始日期"
	        end-placeholder="结束日期"
	      ></el-date-picker>
	    </el-form-item>
		<el-form-item label="审核类型" prop="roomStatus">
		  <el-radio-group v-model="formStaus.roomStatus">
		    <el-radio  :label="2">审核通过</el-radio>
		    <el-radio  :label="9">审核拒绝</el-radio>
		  </el-radio-group>
		</el-form-item>
	    <el-form-item label="备注" prop="notes">
			<el-input v-model="form.notes" placeholder="备注" type="textarea" />
	    </el-form-item>
	  </el-form>
	  <div slot="footer" class="dialog-footer">
	    <el-button type="primary" @click="submitFormStaus">确 定</el-button>
	    <el-button @click="cancel">取 消</el-button>
	  </div>
	</el-dialog>
  </div>
</template>
<script>
import { listRoom, delRoom, insertRoom, editRoom, Examine } from "@/api/house/layoutmanagement";
import { 
	listRent,
	delRent,
	insertRent,
	editRent,
	allListRent
} from "@/api/rent/index";
export default {
  name: "Post",
  dicts: ['sys_normal_disable'],
  data() {
    return {
	  //所有房间
	  RoomList:[],
	  //审核弹窗
	  openStatus:false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
	  //所属小区
	  communityArr:[],
	  //所有楼宇
	  ListFloorArr:[],
	  //上传图片
	  fileList:[],
	  //所有房型
	  HomeTypeList:[],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 岗位表格数据
      postList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
		roomStatus: undefined,
        communityId: undefined,
        floorName: undefined,
      },
      // 表单参数
      form: {
		  roomStatus:2
	  },
	  formStaus: { roomStatus:2 },
	  rulesStatus:{
		  custodyCycle: [
		    { required: true, message: "托管周期不能为空", trigger: "blur" }
		  ],
	  },
      // 表单校验
      rules: {
        roomId: [
          { required: true, message: "房屋不能为空", trigger: "change" }
        ],
        roomPrice: [
          { required: true, message: "价格不能为空", trigger: "blur" }
        ],
        occupancy: [
          { required: true, message: "入住人数不能为空", trigger: "blur" }
        ],
        remark: [
          { required: true, message: "备注不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
	this.getRoomList()
	this.getAllXiaoQu();
	this.getListFloorArr();
	this.getTypeList()
  },
  computed: {
	  rentStatus: {
	    get() {
	      return this.$store.state.common.rentStatus
	    },
	  },
	  supportServices: {
	    get() {
	      return this.$store.state.common.supportServices
	    },
	  },
  },
  methods: {
	//选择配套服务
	checkboxChange() {
	  let checkedAll = []// 存储所有的选中值
	  this.supportServices.forEach(item => {
		// 将选中的数据存储arr中
		item.checked.forEach(kitem => {
		  checkedAll.push(kitem)
		})
	  })
	  // 去重
	  checkedAll = Array.from(new Set(checkedAll))
	  this.ids = checkedAll
	},
	//获取所有房间
	getRoomList(){
		this.$store.dispatch('AllRoomList').then(res => {
			 this.RoomList = res
				 //this.getList();
		})
	},
    //获取所有小区
    getAllXiaoQu(){
		this.$store.dispatch('AllCommunityList').then(res => {
			 this.communityArr = res
			 this.getList();
		})
    },
	//获取所有房型
	getTypeList(){
		this.$store.dispatch('AllHomeTypeList').then(res => {
			 this.HomeTypeList = res
		})
	},
	//获取所有楼宇
	getListFloorArr(){
		this.$store.dispatch('AllFloorList').then(res => {
			 this.ListFloorArr = res
		})
	},
    /** 查询岗位列表 */
    getList() {
      this.loading = true;
      listRent(this.queryParams).then(response => {
        this.postList = response.rows.map(i=>{
			let room = this.RoomList.filter(v=>v.id == i.roomId)[0]
			i.rentStatusCn = this.rentStatus.filter(v=>v.type == i.status)[0]['name']
			i.room = room.communityName +''+ room.floorName +''+ room.units +'单元'+ room.roomNo +'号'
			return i
		})
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
	  this.openStatus = false;
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10
      };
	  this.form = {}
	  this.fileList = []
      this.resetForm("form");
	  this.postList = [...this.postList]
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
	/** 审核状态 */
	handleEditStatus(res){
		const postId = res.id
		this.formStaus.roomId = postId
		this.openStatus = true;
	},
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
	//获取上传图片
	inputFile(file){
		this.form.images = file
	},
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
	  this.supportServices.map(item => {
		item.checked = []
	  })
      this.open = true;
      this.title = "添加挂租";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      const postId = row.id
      this.form = row;
	  this.fileList = row.images
      this.title = "修改挂租";
	  let ids = !!row.supportServices ? row.supportServices.split(',') : []
	  this.supportServices.map(item => {
		item.checked = []
		item.item.forEach(jitem => {
		  if (ids.includes(jitem)) {
			item.checked.push(jitem)
		  }
		})
	  })
	  this.open = true;
    },
    /** 提交按钮 */
    submitForm: function() {
	  if(this.ids.length == 0){
		  this.$modal.msgError("请选择配套设施!");
		  return false
	  }
	  this.form.supportServices = this.ids.join(',')
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            editRent(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            insertRent(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
	/** 审核提交按钮 */
    submitFormStaus: function() {
      this.$refs["formStaus"].validate(valid => {
        if (valid) {
          Examine(this.formStaus).then(response => {
            this.$modal.msgSuccess("操作成功");
            this.openStatus = false;
            this.getList();
          });
        }
      });
    },
    /** 删除按钮操作 */ 
    handleDelete(row) {
      const postIds = {
		  id:row.id
	  };
      this.$modal.confirm('是否确认删除该挂租？').then(function() {
        return delRent(postIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
  }
};
</script>
<style lang="scss" scoped>
	.typeitem{
		padding-right: 20px;
	}
	.shepei{
		margin-top: 30px;
	}
</style>